package com.tanhua.manage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Service
public class FreezeService {

    @Autowired
    private FreezesService freezesService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 冻结用户
     * @param param
     * @return
     */
    public Map<String, String> userFreeze(Map<String, String> param) {
        //新建一个HashMap集合 获取属性
        HashMap<String, String> message = new HashMap<>();
        //向HashMap注入数据
        String userId = param.get("userId");
        String freezingTime = param.get("freezingTime");
        String freezingRange = param.get("freezingRange");
        String reasonsForFreezing = param.get("reasonsForFreezing");
        String frozenRemarks = param.get("frozenRemarks");

        //冻结
        Boolean bool = freezesService.userFreeze(Long.valueOf(userId), Integer.valueOf(freezingTime), Integer.valueOf(freezingRange), reasonsForFreezing, frozenRemarks);
        //设定解冻时间
        String redisFreezesKey = "Freezing_"+userId;  //冻结用户ID和冻结的类型
        //存储冻结时间
        switch (Integer.valueOf(freezingTime)){
            case 1:
                this.redisTemplate.opsForValue().set(redisFreezesKey,freezingRange, Duration.ofDays(3L));
                break;
            case 2:
                this.redisTemplate.opsForValue().set(redisFreezesKey,freezingRange, Duration.ofSeconds(60L));
                break;
            case 3:
                this.redisTemplate.opsForValue().set(redisFreezesKey,freezingRange);
                break;
        }

        if (!bool) {
            message.put("message", "操作失败!");
            return message;
        }
        message.put("message", "操作成功!");
        return message;
    }


    //解冻
    public Map<String, String> userUnfreeze(Map<String, String> param) {
        HashMap<String, String> message = new HashMap<>();
        String userId = param.get("userId");
        String reasonsForThawing = param.get("frozenRemarks");
        System.out.println("reasonsForThawing:"+reasonsForThawing);

        String redisFreezesKey = "Freezing_"+userId;  //冻结用户ID和冻结的类型
        this.redisTemplate.delete(redisFreezesKey);  //删除redis中的冻结状态

        Boolean bool = freezesService.userUnfreeze(Long.valueOf(userId), reasonsForThawing);
        if (!bool) {
            message.put("message", "操作失败!");
            return message;
        }
        message.put("message", "操作成功!");
        return message;
    }

}
